//
//  ZDWorkDetailCell.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/23.
//
//

#import "ZDWorkDetailCell.h"
#import "ZDFeeCell.h"

@interface ZDWorkDetailCell () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *status1Button;
@property (weak, nonatomic) IBOutlet UIButton *statusButton;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *singleLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *fangLabel;
@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;

@property (weak, nonatomic) IBOutlet UIButton *selectButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightMarginConstraint;


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;

- (IBAction)signAction:(id)sender;

@end

@implementation ZDWorkDetailCell

- (void)awakeFromNib {
    UIView *containerView = self.contentView.subviews.firstObject;
    containerView.layer.shadowColor = [UIColor grayColor].CGColor;
    containerView.layer.shadowOpacity = 0.2;
    containerView.layer.shadowOffset = CGSizeMake(0, 2);
    [self.statusButton setBackgroundImage:[UIImage imageWithColor:kGlobalColor] forState:UIControlStateNormal];
    [self.statusButton setBackgroundImage:[UIImage imageWithColor:BMColor(176, 176, 176, 1)] forState:UIControlStateDisabled];
    self.statusButton.layer.cornerRadius = 15.f;
    self.statusButton.layer.masksToBounds = YES;
    self.tableView.rowHeight = 22.f;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDFeeCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _serviceLog.typeCharges.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDFeeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.fee = _serviceLog.typeCharges[indexPath.row];
    return cell;
}

- (void)setServiceLog:(ServiceLog *)serviceLog
{
    _serviceLog = serviceLog;
    
    if (_serviceLog.selected) {
        _selectButton.hidden = NO;
        _rightMarginConstraint.priority = 800;
        _selectButton.selected = _serviceLog.agreeSignAll;
    } else {
        _selectButton.hidden = YES;
        _rightMarginConstraint.priority = 700;
    }
    
    _statusButton.enabled = YES;
    [_status1Button setImage:[UIImage imageNamed:@"status_work1"] forState:UIControlStateNormal];
    switch (_serviceLog.status) {
        case 0:
            [_status1Button setTitle:@"待签收" forState:UIControlStateNormal];
            [_statusButton setTitle:@"签收" forState:UIControlStateNormal];
            break;
        case 1:
            [_status1Button setTitle:@"待结算" forState:UIControlStateNormal];
            [_statusButton setTitle:@"结算" forState:UIControlStateNormal];
            break;
        case 2:
            [_status1Button setTitle:@"已结算" forState:UIControlStateNormal];
            [_status1Button setImage:[UIImage imageNamed:@"status_work0"] forState:UIControlStateNormal];
            _statusButton.enabled = NO;
            break;
    }
    _priceLabel.text = [NSString stringWithFormat:@"￥%.1f", _serviceLog.realsumPrice];
    _dateLabel.text = [NSString stringWithFormat:@"服务时间：%@", _serviceLog.startTime];
    _distanceLabel.text = [NSString stringWithFormat:@"服务距离：%@", _serviceLog.distance ? _serviceLog.distance : @"无"];
    _fangLabel.text = [NSString stringWithFormat:@"服务方量：%.1f方", _serviceLog.volume];
    _remarkLabel.text = [NSString stringWithFormat:@"服务说明：%@", _serviceLog.remarks ? _serviceLog.remarks : @"无"];
    
    double price = _serviceLog.realsumPrice;
    for (TypeCharge *fee in _serviceLog.typeCharges) {
        price -= fee.charge;
    }
    _singleLabel.text = [NSString stringWithFormat:@"方量单价：%.1f元/方", price / _serviceLog.volume];
    
    [self.tableView reloadData];
    _heightConstraint.constant = self.tableView.contentSize.height;
}

- (IBAction)signAction:(id)sender {
    if (_signOP) {
        _signOP(_serviceLog.status);
    }
}

- (IBAction)selectAction:(UIButton *)sender {
    if (_agreeSign) {
        _agreeSign();
    }
}

@end
